﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class NhaXuatBan_DAO
    {
        clsDocSQL m_clsDocSQL = new clsDocSQL();
        public NhaXuatBan_DTO[] DanhSachNhaXuatBan()
        {
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure("sp_LayDanhSachNXB");
            NhaXuatBan_DTO[] arrsach;
            int i, n = dt.Rows.Count;
            arrsach = new NhaXuatBan_DTO[n];
            for (i = 0; i < n; i++)
            {
                object sach = Getdatarow(dt, i);
                arrsach[i] = (NhaXuatBan_DTO)sach;
            }
            return arrsach;
        }
        public NhaXuatBan_DTO DanhSachNhaXuatBan_TheoMaNXB(String str_MaNXB)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[1];
            m_SqlParameter[0] = new SqlParameter("@MaNXB", str_MaNXB);
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure1("sp_LayDanhSachNXB_TheoMaNXB", m_SqlParameter);
            NhaXuatBan_DTO arrsach = new NhaXuatBan_DTO();
                object sach = Getdatarow(dt, 0);
                arrsach = (NhaXuatBan_DTO)sach;
            return arrsach;
        }
        protected object Getdatarow(DataTable dt, int dong)
        {
            NhaXuatBan_DTO nxb_dto = new NhaXuatBan_DTO();
            nxb_dto.Str_MaNXB = dt.Rows[dong]["MaNXB"].ToString();
            nxb_dto.Str_TenNXB = dt.Rows[dong]["TenNXB"].ToString();
            nxb_dto.Str_DiaChi = dt.Rows[dong]["DiaChi"].ToString();
            nxb_dto.Str_DienThoai = dt.Rows[dong]["DienThoai"].ToString();
            nxb_dto.Str_Email = dt.Rows[dong]["Email"].ToString();
            nxb_dto.Str_TrangThai = int.Parse(dt.Rows[dong]["TrangThai"].ToString());
            return (object)nxb_dto;
        }
        public String MaTang()
        {
            String kq = "";
            kq = m_clsDocSQL.DocKieuString("MaNXB_Tang");
            return kq.ToString();
        }
        public int ThemNXB(NhaXuatBan_DTO nxb)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[7];
            m_SqlParameter[0] = new SqlParameter("@MaNXB", nxb.Str_MaNXB);
            m_SqlParameter[1] = new SqlParameter("@TenNXB", nxb.Str_TenNXB);
            m_SqlParameter[2] = new SqlParameter("@DiaChi", nxb.Str_DiaChi);
            m_SqlParameter[3] = new SqlParameter("@DienThoai", nxb.Str_DienThoai);
            m_SqlParameter[4] = new SqlParameter("@Email", nxb.Str_Email);
            m_SqlParameter[5] = new SqlParameter("@TrangThai", nxb.Str_TrangThai);
            m_SqlParameter[6] = new SqlParameter("@KQ", 0);
            m_SqlParameter[6].Direction = ParameterDirection.Output;
            int KQ = m_clsDocSQL.Insert_Delete_Update("sp_ThemNXB", m_SqlParameter);
            return KQ;
        }
        public int XoaNXB(NhaXuatBan_DTO nxb)//
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[2];
            m_SqlParameter[0] = new SqlParameter("@MaNXB", nxb.Str_MaNXB);
            m_SqlParameter[1] = new SqlParameter("@KQ", 0);
            m_SqlParameter[1].Direction = ParameterDirection.Output;
            int kq = m_clsDocSQL.Insert_Delete_Update("sp_XoaNXB", m_SqlParameter);
            return kq;
        }

        public int CapNhatNXB(NhaXuatBan_DTO nxb)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[6];
            m_SqlParameter[0] = new SqlParameter("@MaNXB", nxb.Str_MaNXB);
            m_SqlParameter[1] = new SqlParameter("@TenNXB", nxb.Str_TenNXB);
            m_SqlParameter[2] = new SqlParameter("@DiaChi", nxb.Str_DiaChi);
            m_SqlParameter[3] = new SqlParameter("@DienThoai", nxb.Str_DienThoai);
            m_SqlParameter[4] = new SqlParameter("@Email", nxb.Str_Email);
            m_SqlParameter[5] = new SqlParameter("@KQ", 0);
            m_SqlParameter[5].Direction = ParameterDirection.Output;
            int kq = m_clsDocSQL.Insert_Delete_Update("sp_CapNhatNXB", m_SqlParameter);
            return kq;
        }
    }
}
